/*
 * @Date: 2019-12-2
 * @Author: xuechengwu <xuechengwu@erayt.com>
 * @Description: 分时背景层
 */
import Layer from '../core/Layer';
import Point from '../core/Point';
import MultiLine from '../base/MultiLine';
import DashLine from '../base/DashLine';
import Text from '../base/Text';
import Color from '../core/Color';

export default class SymmetryLayer extends Layer {
  constructor(canvas, options) {
    super(canvas);
    this.width = options.width || canvas.width;
    this.height = options.height || canvas.height;
    this.data = options.data || [];
    this.thresholds = options.thresholds || [];
    this.base = options.base;
    this.xStep = options.xStep || 1;
    this.color = options.color || '#9E9E9E';
  }

  /**
   * 渲染
   */
  make() {
    if (this.data.length === 0) {
      return;
    }
    this.childs.splice(0, this.childs.length);
    let max = -1000000000000;
    let min = 10000000000000;
    for (let i = 0; i < this.data.length; i++) {
      let line = this.data[i];
      for (let j = 0; j < line.data.length; j++) {
        if (line.data[j] > max) {
          max = line.data[j];
        }
        if (line.data[j] < min) {
          min = line.data[j];
        }
      }
    }
    for (let i = 0; i < this.thresholds; i++) {
      let threshold = this.thresholds[i];
      if (threshold > max) {
        max = threshold;
      }
      if (threshold < min) {
        min = threshold;
      }
    }
    if (this.base === undefined) {
      this.base = (max + min) / 2;
    }
    let maxDiff = Math.abs(max - this.base);
    if (maxDiff < Math.abs(min - this.base)) {
      maxDiff = Math.abs(min - this.base);
    }
    maxDiff = maxDiff * 1.1;
    let yMax = this.base + maxDiff;
    let yMin = this.base - maxDiff;
    let yStep = this.height / (yMax - yMin);
    for (let i = 0; i < this.data.length; i++) {
      let line = this.data[i];
      let points = [];
      for (let j = 0; j < line.data.length; j++) {
        points.push(
          new Point(
            this.xStep * j + this.position.x,
            (line.data[j] - yMin) * yStep + this.position.y
          )
        );
      }
      let multiLine = new MultiLine(this.canvas, points);
      multiLine.setColor(new Color(line.color));
      this.addChild(multiLine);
    }
    for (let i = 0; i < this.thresholds.length; i++) {
      let threshold = this.thresholds[i];
      let dashLine = new DashLine(this.canvas, {
        from: new Point(this.position.x, this.position.y + (threshold - yMin) * yStep),
        to: new Point(this.position.x + this.width, this.position.y + (threshold - yMin) * yStep),
        color: this.color
      });
      this.addChild(dashLine);
      let txt = new Text(this.canvas, {
        text: threshold,
        font: 'PingFang SC',
        size: 20,
        color: this.color
      });
      txt.setPosition(this.position.x, this.position.y + (threshold - yMin) * yStep - 20 * 0.5);
      this.addChild(txt);
    }
    let baseLine = new DashLine(this.canvas, {
      from: new Point(this.position.x, this.position.y + (this.base - yMin) * yStep),
      to: new Point(this.position.x + this.width, this.position.y + (this.base - yMin) * yStep),
      color: this.color
    });
    let baseTxt = new Text(this.canvas, {
      text: this.base,
      font: 'PingFang SC',
      size: 20,
      color: this.color
    });
    baseTxt.setPosition(this.position.x, this.position.y + (this.base - yMin) * yStep - 20 * 0.5);
    this.addChild(baseLine, baseTxt);
  }
}
